<?php
class Clients_model extends CRM_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function GetDS(){
		$this->db->select('_clients.*, 
							DATE_FORMAT(_clients.birthday, \'%d.%m.%Y\') as birthday,
							DATE_FORMAT(_clients.f_pass_period, \'%d.%m.%Y\') as f_pass_period,
							_cities.city_name as city_name,
							DATE_FORMAT(_clients.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT');
		$this->db->from('_clients');
		$this->db->join('_cities', '_clients._cities_rid = _cities.rid');
		if($searchRule = $this->ciObject->GetSessionData('searchrule')){
			$this->db->like($searchRule);
		}
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_clients');
		
		if($query->num_rows()>0){
			return $this->ciObject->GetPagingDS($query->result());
		}
		return array();
	}
	
	public function GetCitiesList(){
		$this->db->select('_cities.rid, _cities.city_name as city_name');
		$this->db->from('_cities');
		$this->db->orderby('city_name');
		$query = $this->DBGet('_cities');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetClientsList(){
		$this->db->select('_clients.rid, CONCAT(_clients.f_name, \' \', _clients.s_name, \' \', _clients.l_name) as client_name');
		$this->db->from('_clients');
		$this->db->orderby('client_name');
		$query = $this->DBGet('_clients');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetCountriesList(){
		$this->db->select('_countries.rid, _countries.country_name as country_name');
		$this->db->from('_countries');
		$this->db->orderby('country_name');
		$query = $this->DBGet('_countries');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetEditDS(){
		$this->db->select('_clients.*, DATE_FORMAT(_clients.birthday, \'%d.%m.%Y\') as birthday, DATE_FORMAT(_clients.f_pass_period, \'%d.%m.%Y\') as f_pass_period');
		$this->db->from('_clients');
		$this->db->where(array('_clients.rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_clients');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insArray = array('parent'=>$_POST['parent'],                        
							'f_name'=>$_POST['f_name'],                                  
							's_name'=>$_POST['s_name'],                         
							'l_name'=>$_POST['l_name'],                           
							'f_name_lat'=>$_POST['f_name_lat'],                             
							'l_name_lat'=>$_POST['l_name_lat'],                    
							'birthday'=>$this->toMySQLDate($_POST['birthday']),                
							'citizenship'=>$_POST['citizenship'],                   
							'passp_seria'=>$_POST['passp_seria'],              
							'passp_num'=>$_POST['passp_num'],              
							'f_pass_seria'=>$_POST['f_pass_seria'], 
							'f_pass_num'=>$_POST['f_pass_num'], 
							'f_pass_period'=>$this->toMySQLDate($_POST['f_pass_period']),
							'nal_number'=>$_POST['nal_number'],           
							'_cities_rid'=>$this->GetFilialCityRid(),  
							'card_num'=>$_POST['card_num'],                             
							'adress'=>$_POST['adress'],                               
							'phones'=>$_POST['phones'],                         
							'email'=>$_POST['email'],             		
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->insert('_clients', $insArray);
		$insRid = $this->db->insert_id();
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $insRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateArray = array('parent'=>$_POST['parent'],                        
							'f_name'=>$_POST['f_name'],                                  
							's_name'=>$_POST['s_name'],                         
							'l_name'=>$_POST['l_name'],                           
							'f_name_lat'=>$_POST['f_name_lat'],                             
							'l_name_lat'=>$_POST['l_name_lat'],                    
							'birthday'=>$this->toMySQLDate($_POST['birthday']),                     
							'citizenship'=>$_POST['citizenship'],                   
							'passp_seria'=>$_POST['passp_seria'],              
							'passp_num'=>$_POST['passp_num'],              
							'f_pass_seria'=>$_POST['f_pass_seria'], 
							'f_pass_num'=>$_POST['f_pass_num'], 
							'f_pass_period'=>$this->toMySQLDate($_POST['f_pass_period']),
							'nal_number'=>$_POST['nal_number'],           
							'_cities_rid'=>$this->GetFilialCityRid(),                           
							'adress'=>$_POST['adress'],    
							'card_num'=>$_POST['card_num'],                           
							'phones'=>$_POST['phones'],                         
							'email'=>$_POST['email'],       
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->update('_clients', $updateArray, array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_clients', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function GetFilialCityRid(){
		$this->db->select('_cities.rid');
		$this->db->from('_cities');
		$this->db->join('_filials', '_filials._cities_rid=_cities.rid');
		$this->db->where(array('_filials.rid'=>$this->user->GetFilialRid()));
		$query = $this->db->get();
		if($query->num_rows()) return $query->row()->rid;
		return False;	
	}
}
?>